<script>
	var orderItemRowId = 0;
	function selectCustomer(index,row) {
		printLog(row);
		$('#orderAddress').val(row.address);
		$('#orderContacts').val(row.contacts);
		$('#orderMobilePhone').val(row.mobilePhone);
		$('#orderOpeartor').val(row.opeartor);
		$('#orderOpeartorName').val(row.opeartorName);
		$('#orderCustomerName').val(row.name);
		$('#orderCustomerCode').val(row.code);
		$('#orderCustomerCode').validatebox('options').validType="remote['customerInfo.do?checkCustomerCodeIsExit&id="+row.id+"','code']";

		$('#orderCustomerLevelId').combobox("clear").combobox('reload','customerLevel.do?loadCustomerLevelComboBox&value='+row.customerLevelId);
		$('#orderCustomerAreaId').combobox("clear").combobox('reload','baseArea.do?loadBaseAreaComboBox&value='+row.customerAreaId);
	}
	
	function addOrderItem() {
		
		createFromWindowOptions({
			title:"新增信息",
			maximizable:true,
			width:700,
			height:300,
			href:"orderItem.do?editOrderItem",
			callback:function(d) {
				var rowData = d.form("getData");
				printLog(rowData);
				orderItem_list.datagrid("appendRow", rowData);
				calculateOrderAmount();
			},
			enableSaveButton:true
		});
	}
	
	function calculateOrderAmount() {
		var list = orderItem_list.datagrid('getRows');
		printLog(list);
		var totalItemAmount = Number('0');
		var totalNum = Number('0');
		for(var i=0; i<list.length; i++){
			printLog(list[i].totalAmount);
			totalItemAmount = totalItemAmount+Number(list[i].totalAmount);
			totalNum = totalNum+Number(list[i].countNum);
		}
		printLog(totalItemAmount);
		totalItemAmount =  Number(totalItemAmount)-Number($('#preferentialAmount').numberbox('getValue'))
		$('#orderAmount').numberbox('setValue', Number(totalItemAmount).toFixed(2));
		$('#orderTotalNum').numberbox('setValue', Number(totalNum).toFixed(0));
	}
	
	function calculateTotal(value,id) {
		$('#'+id).numberbox('setValue', value);
		calculateOrderAmount();
	}
	
	function deleteOrderItem() {
		var rows = orderItem_list.datagrid('getChecked');
	    if (rows&&rows.length>0){
	        $.messager.confirm('询问','你确认要删除吗?',function(r){
	            if (r){
	            	var rowIndex = "";
	            	for(var i=rows.length-1; i>=0; i--) {
	            		rowIndex = orderItem_list.datagrid('getRowIndex', rows[i]);
	            		orderItem_list.datagrid('deleteRow', rowIndex);
	            	}
	            	calculateOrderAmount();
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}
	
	function preEditOrderItem() {
		var rows = orderItem_list.datagrid('getChecked');
		if (rows&&rows.length>0) {
			if(rows.length>1) {
				showMsg('请选择一条操作记录.');
				return;
			}
			var rowIndex = orderItem_list.datagrid('getRowIndex', rows[0]);
// 			rows[0].rowIndex = rowIndex;
			printLog('rowIndex='+rowIndex);
			createFromWindowOptions({
				title:"编辑信息",
				maximizable:true,
				width:700,
				height:300,
				href:"orderItem.do?preEditOrderItem&"+parseParam(rows[0]),
				callback:function(d) {
					var rowData = d.form("getData");
					printLog(rowData);
					orderItem_list.datagrid("updateRow", {index: rowIndex,row:rowData});
// 					orderItem_list.datagrid("appendRow", rowData);
					calculateOrderAmount();
				},
				enableSaveButton:true
			});
		} else {
			showMsg('请选择操作记录.');
		}
	}
	
	function getOrderItemRow() {
        return {id:++orderItemRowId,projectId:1,projectName:'',materialId:1,materiaName:"XX",doContent:"NAME",countNum:1,unitPrice:1,unitName:"米",totalAmount:12,remark:'remark'};
    }
	
	function onDblClickOrderItemRow(rowIndex, rowData) {
		createFromWindowOptions({
			title:"编辑信息",
			maximizable:true,
			width:700,
			height:300,
			href:"orderItem.do?preEditOrderItem&"+parseParam(rowData),
			callback:function(d) {
				var rowData2 = d.form("getData");
				orderItem_list.datagrid("updateRow", {index: rowIndex,row:rowData2});
				calculateOrderAmount();
			},
			enableSaveButton:true
		});
	}
	
	function clearCustomerInfo() {
		$('#orderAddress').val("");
		$('#orderContacts').val("");
		$('#orderMobilePhone').val("");
		$('#orderOpeartor').val("");
		$('#orderOpeartorName').val("");
		$('#orderCustomerName').val("");
		$('#orderCustomerCode').val("");
		$('#orderCustomerCode').validatebox('options').validType="remote['customerInfo.do?checkCustomerCodeIsExit&id=0','code']";

	}
	function addOrderCustomer() {
		var btnText = $('#orderAddCustomer').linkbutton().text().trim();
		printLog(btnText);
		
		var nowText = (btnText=='新客户'?'重选':'新客户');
		var iconCls = (btnText=='新客户'?'icon-reload':'icon-add');
// 		printLog(btnText=='新客户');
		setCustomerInfo(nowText);
		$('#orderAddCustomer').linkbutton({text:nowText,iconCls:iconCls});
	}
	
	function setCustomerInfo(btnText) {
		clearCustomerInfo();
		var editable = btnText=='新客户';
		$('#isNewCustomer').val(!editable+'');
		$('#orderAddress').attr('readonly',editable).validatebox({required: !editable});  //设置输入框为禁用
		$('#orderContacts').attr('readonly',editable).validatebox({required: !editable}); 
		$('#orderMobilePhone').attr('readonly',editable).validatebox({required: !editable}); 
// 		$('#orderOpeartor').attr('readonly',editable); 
// 		$('#orderOpeartorName').attr('readonly',editable); 
		$('#orderCustomerName').attr('readonly',editable).validatebox({required: !editable}); 
		$('#orderCustomerCode').attr('readonly',editable).validatebox({required: !editable}); 
		var enable = editable?'disable':'enable';
		$('#orderCustomerLevelId').combobox({required: !editable, disabled: editable}).combobox("clear").combobox('reload','customerLevel.do?loadCustomerLevelComboBox&value=');
		$('#orderCustomerAreaId').combobox({required: !editable, disabled: editable}).combobox("clear").combobox('reload','baseArea.do?loadBaseAreaComboBox&value=');
		$('#customerId').combogrid({required: editable, disabled: !editable}).combogrid("clear");
	}
</script>
<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'north',border:false,height:150">
		<table cellpadding="5">
			<input name="id" type="hidden" value="${(orderItem.id)!}" />
			<input name="orderType" type="hidden" value="${(orderItem.orderType)!}" />
			
			<tr>
				<td class="tbLabel">订单编号:</td>
				<td>
					<input name="no"  class="easyui-validatebox textbox" data-options="required: false" readonly="readonly" value="${(orderItem.no)!}"/>
				</td>
				<td class="tbLabel">状态:</td>
				<td>
					<@eu.comboBox url="common.do?loadComboBox&key=ORDER_STATUS&value=${(orderItem.status)!}" name="status" disabled="true"/>
<!-- 					<input name="status"  class="easyui-validatebox textbox" data-options="required: false" value="${(orderItem.status)!}"/> -->
				</td>
			
				<td class="tbLabel">付款情况:</td>
				<td>
					<@eu.comboBox url="common.do?loadComboBox&key=PAYMENT_STATUS&value=${(orderItem.receivables)!'1'}" name="receivables" disabled="true"/>
				</td>
				<td class="tbLabel">开单人:</td>
				<td>
<!-- 					<@eu.combogrid width="162" id="creatorCombogrid" idField="loginName" textField="name" name="creator" url="user.do?loadByCondition" item="com.xdtech.sys.vo.UserItem" values="'${(orderItem.creator)!}'" disabled="true"/> -->

					<input name="creatorName"  class="easyui-validatebox textbox" data-options="required: false" value="${(orderItem.creatorName)!}" readonly="readonly"/>
					<input name="creator"  type="hidden" value="${(orderItem.creator)!}" />
					
				</td>
				<td class="tbLabel">开单时间:</td>
				<td>
					<input name="createOrderTime" class="easyui-datetimebox" style="width:160px;" data-options="required: true,disabled:true" value="${(orderItem.createOrderTime)!}"/>
				</td>
			</tr>
			<tr id="customerSelectList">
				<td class="tbLabel">客户选择:</td>
				<td colspan="7">
				<@eu.combogrid id="customerId" idField="id"
					textField="name" name="customerId" width="890"
					url="customerInfo.do?loadList" keyword="code"
					item="com.xdtech.ad.vo.CustomerInfoItem" disabled="${disabled}"
					values="${(orderItem.customerId)!}" required="true"
					onSelect="selectCustomer(index,row)"/>				
				</td>
				<td colspan="2">
					<@eu.linkbutton id="orderAddCustomer" showName="新客户" shiro="ad:order:addCustomer" iconCls="icon-add" onclick="addOrderCustomer()"/>
				</td>
				
			</tr>
			<tr id="newCustomerEdite">
				<input id="isNewCustomer" name="isNewCustomer"  type="hidden" value="false"/>
				
				<td class="tbLabel">客户编码:</td>
				<td>
					<input id="orderCustomerCode" name="orderCustomerCode" readonly="readonly" class="easyui-validatebox textbox" data-options="required: false" value="${(orderItem.orderCustomerCode)!}" validType="remote['customerInfo.do?checkCustomerCodeIsExit&id=${(orderItem.customerId)!}','code']" invalidMessage="客户编码已存在"/>
				</td>
				<td class="tbLabel">客户名称:</td>
				<td>
					<input id="orderCustomerName" name="customerName" readonly="readonly" class="easyui-validatebox textbox" data-options="required: false" value="${(orderItem.customerName)!}"/>
				</td>
				<td class="tbLabel">归属区域:</td>
				<td>
					<@eu.comboBox id="orderCustomerAreaId" disabled="true" name="orderCustomerAreaId" required="false" url="baseArea.do?loadBaseAreaComboBox&value=${(orderItem.orderCustomerAreaId)!}"/>
				</td>
				<td class="tbLabel">客户级别:</td>
				<td>
					<@eu.comboBox id="orderCustomerLevelId" disabled="true" name="orderCustomerLevelId" required="false" url="customerLevel.do?loadCustomerLevelComboBox&value=${(orderItem.orderCustomerLevelId)!}"/>		
				</td>
				<td class="tbLabel">系统账号:</td>
				<td>
					<input id="orderOpeartor" name="opeartor"  type="hidden" value="${(orderItem.opeartor)!}"/>
					<input id="orderOpeartorName" name="opeartorName" readonly="readonly"  class="easyui-validatebox textbox" data-options="required: false" value="${(orderItem.opeartorName)!}"/>
				</td>
			</tr>
			<tr>
				<td class="tbLabel">地址:</td>
				<td colspan="3">
					<input id="orderAddress" name="address" readonly="readonly" class="easyui-validatebox textbox" style="width:390px" data-options="required: false" value="${(orderItem.address)!}"/>
				</td>
				<td class="tbLabel">联系人:</td>
				<td>
					<input id="orderContacts" name="contacts" readonly="readonly" class="easyui-validatebox textbox" data-options="required: false" value="${(orderItem.contacts)!}"/>
				</td>
				<td class="tbLabel">联系人电话:</td>
				<td>
					<input id="orderMobilePhone" name="mobilePhone" readonly="readonly"  class="easyui-validatebox textbox" data-options="required: false" value="${(orderItem.mobilePhone)!}"/>
				</td>
				<td class="tbLabel">交货时间:</td>
				<td>
					<input name="deliveryDate" class="easyui-datebox" style="width:160px;" data-options="required: false" value="${(orderItem.deliveryDate)!}"/>
				</td>
				
			</tr>
		</table>
	</div>
	<div data-options="region:'center',border:false">
		<div class="easyui-tabs" data-options="fit:true">
			<div title="商品明细" data-options="closable:false,refreshable:false,height:300" > 
	            <@eu.datagrid id="orderItem_list" toolbar="#orderItemlist_search" idField="id"
				 url="orderItem.do?loadList&orderId=${(orderItem.id)!-1}" rownumbers="true" pagination="false" autoEditing="false" onDblClickRow="onDblClickOrderItemRow(rowIndex, rowData)"
				 item="com.xdtech.ad.vo.OrderItemItem"/>
				 <div id="orderItemlist_search">
					<div id="orderItem_toolbar" style="height: auto">
						<@eu.linkbutton showName="新增" shiro="ad:orderItem:add" iconCls="icon-add" onclick="addOrderItem()"/>
						<@eu.linkbutton showName="删除" shiro="ad:orderItem:delete" iconCls="icon-remove" onclick="deleteOrderItem()"/>
						<@eu.linkbutton showName="编辑" shiro="ad:orderItem:preEdit" iconCls="icon-edit" onclick="preEditOrderItem()"/>						
					</div>
				</div>
	        </div>
		</div>
	</div>
	<div data-options="region:'south',border:false,height:150">
		<table cellpadding="5">
			<tr>
				<td class="tbLabel">付款方式:</td>
				<td>
					<@eu.comboBox url="common.do?loadComboBox&key=PAY_TYPE&value=${(orderItem.payType)!'ZFB'}" name="payType"/>
		<!-- 			<input name="payType"  class="easyui-validatebox textbox" data-options="required: false" value="${(orderItem.payType)!}"/> -->
				</td>
			
				<td class="tbLabel">预付款:</td>
				<td>
					<input name="prePayment" style="width:158px;" class="easyui-numberbox"  precision="2" data-options="required: false" value="${(orderItem.prePayment)!}"/>
				</td>
				<td class="tbLabel">物流公司:</td>
				<td>
					<@eu.comboBox url="common.do?loadComboBox&key=LOGISTICS_COMPANY_TYPE&value=${(orderItem.logisticsCompany)!'SFKD'}" name="logisticsCompany"/>
					
<!-- 					<input  name="logisticsCompany"  class="easyui-validatebox textbox" data-options="required: false" value="${(orderItem.logisticsCompany)!}"/> -->
				</td>
				<td class="tbLabel">物流单号:</td>
				<td>
					<input  name="trackingNum"  class="easyui-validatebox textbox" data-options="required: false" value="${(orderItem.trackingNum)!}"/>
				</td>
			</tr>
			<tr>
				<td class="tbLabel">优惠金额:</td>
				<td>
					<input name="preferentialAmount" id="preferentialAmount" style="width:158px;" class="easyui-numberbox"  precision="2" data-options="required: false,onChange:function(newValue,oldValue) {calculateTotal(newValue,'preferentialAmount');}" value="${(orderItem.preferentialAmount)!0}"/>
				</td>
			
				<td class="tbLabel">金额:</td>
				<td>
					<input id="orderAmount" name="amount" style="width:158px;" class="easyui-numberbox"  precision="2" data-options="required: false" value="${(orderItem.amount)!}"/>
				</td>
				<td class="tbLabel">总数量:</td>
				<td>
					<input id="orderTotalNum" name="totalNum" style="width:158px;" class="easyui-numberbox"  precision="0" data-options="required: false,disabled:true" value="${(orderItem.totalNum)!}"/>
				</td>
			</tr>
			<tr>
				<td class="tbLabel">备注:</td>
				<td colspan="3">
					<textarea style="height: 50px;width: 418px;" rows="6"  name="remark" class="easyui-validatebox textbox">${(orderItem.remark)!}</textarea>
				</td>
			</tr>
			
		</table>
	</div>
</div>
	
